﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using IPTV.DBUtility;
using IPTV.module;
using System.Data;

/// <summary>
/// Summary description for PlateDao
/// </summary>
namespace IPTV.dao
{
    public class SubjectDao
    {
        public SubjectDao()
        {
            
        }

        public string getSubjectBg(string categorykind)
        {

            string sql = " select b.backgroundpic,i.id from me_p_recommend_config rc,itv_zhzt_info "
            + " i,me_zt_baseinfo b where rc.entity_id = b.zt_id and b.zhztinfoid = i.id  and  "
            + " rc.categorykind = @categorykind and rc.is_publish = 1 and b.deleteflag = 0 and "
            + " b.publishstate = 1 ORDER BY rc.displayorder desc, rc.refreshtime desc ";
           
            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@categorykind" , MySqlDbType.VarChar, 30 )
                };

            parms[0].Value = categorykind;
            String bgUrl = null;
            try
            {
                MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, parms);

                while (reader.Read())
                {
                    bgUrl = reader[0].ToString();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("findTeacherById run error.");
            }

            return bgUrl;

        }

        public List<me_zt_baseinfo> getZtByCategorykind(string categorykind)
        {

            string sql = " select b.backgroundpic,i.id , b.headline from me_p_recommend_config rc,itv_zhzt_info i, "
            + " me_zt_baseinfo b where rc.entity_id = b.zt_id and b.zhztinfoid = i.id  and  "
            + " rc.categorykind = @categorykind and rc.is_publish = 1 and b.deleteflag = 0 and "
            + " b.publishstate = 1 ORDER BY rc.displayorder desc, rc.refreshtime desc ";

            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@categorykind" , MySqlDbType.VarChar, 30 )
                };

            parms[0].Value = categorykind;
            List<me_zt_baseinfo> ztList = new List<me_zt_baseinfo>();
            try
            {
                MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, parms);

                while (reader.Read())
                {
                    me_zt_baseinfo zt = new me_zt_baseinfo();
                    zt.Backgroundpic = reader[0].ToString();
                    zt.Zhztinfoid = reader[1].ToString();
                    zt.Headline = reader[2].ToString();
                    ztList.Add(zt);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return ztList;

        }

        public List<me_tgsg_category> getZsdById(string gradeid, string subjectid)
        {

            string sql = " select id,name,picture,teachername,lessontime,description from "
            + " me_tgsg_category where gradeid = @gradeid and  "
            + " subjectid = @subjectid and deleteflag = 0 order by "
            + " displayorder asc,refreshtime desc ";

            MySqlParameter[] parms = new MySqlParameter[] 
                { 
                    new MySqlParameter( "@gradeid" , MySqlDbType.VarChar, 36 ),
                    new MySqlParameter( "@subjectid" , MySqlDbType.VarChar, 36 )
                };

            parms[0].Value = gradeid;
            parms[1].Value = subjectid;
            List<me_tgsg_category> tgsgList = new List<me_tgsg_category>();
            try
            {
                MySqlDataReader reader = MySQLHelper.ExecuteReader(MySQLHelper.CONNECTION_STRING, sql, parms);

                while (reader.Read())
                {
                    me_tgsg_category tg = new me_tgsg_category();
                    tg.Id = reader[0].ToString();
                    tg.Name = reader[1].ToString();
                    tg.Picture = reader[2].ToString();
                    tg.Teachername = reader[3].ToString();
                    tg.Lessontime = reader[4].ToString();
                    tg.Description = reader[5].ToString();
                    tg.Subjectid = subjectid;
                    tgsgList.Add(tg);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return tgsgList;
        }
    }
}